using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Reflection;
using System.Text;
using System.Web.UI;

using Castle.MonoRail.Framework.Helpers;

using WOODIFramework.Model;

namespace WOODIFramework.Model.Helpers
{
    public class DDDPageHelper
    {
        public string BuildPage(string PageName)
        {
            StringBuilder pageText = new StringBuilder();
            HtmlHelper htmlHelper = new HtmlHelper();
            FormHelper formHelper = new FormHelper();

            Model.Page page = Model.Page.FindPageByPageName(PageName);
            pageText.Append(page.RenderHTML());
            /*
            if (page.GetType().ToString() == "WOODIFramework.Model.FormPage")
            { 
                FormPage formPage = (FormPage)page;
                pageText.Append(htmlHelper.Form(formPage.Action.ToString()));
               
            }

            pageText.Append(BuildTable(page));

            if (page.GetType().ToString() == "WOODIFramework.Model.FormPage")
            {
                pageText.Append(htmlHelper.EndForm()); 
            }
            */
            return pageText.ToString();
        }

        private string ProcessPageItem(Object pageItem, Page page)
        {
            string pageHTML = string.Empty;
            page.BoundObjects = ((Model.PageItem)pageItem).BoundObjects;

            switch (pageItem.GetType().ToString())
            {
                case "WOODIFramework.Model.Label":
                    pageHTML = ((Model.Label)pageItem).RenderHTML();
                    break;
                case "WOODIFramework.Model.TextBox":
                    pageHTML = ProcessTextBox((Model.TextBox)pageItem, page);
                    break;
                case "WOODIFramework.Model.SubmitButton":
                    pageHTML = ProcessSubmitButton((Model.SubmitButton)pageItem, page);
                    break;
                case "WOODIFramework.Model.BasicGrid":
                    pageHTML = ((Model.BasicGrid)pageItem).RenderHTML();
                    break;
            }
            return pageHTML;
        }

        private string ProcessLabel(Model.Label LabelItem, Page page)
        {
            StringBuilder stringBuilder = new StringBuilder();
            StringWriter stringWriter = new StringWriter(stringBuilder);
            HtmlTextWriter writer = new HtmlTextWriter(stringWriter);

            writer.WriteBeginTag("span");
            writer.WriteAttribute("id", LabelItem.Id.ToString());
            writer.WriteAttribute("name", LabelItem.Name);
            writer.WriteAttribute("name", LabelItem.Tooltip);
            writer.Write(HtmlTextWriter.TagRightChar);
            if (LabelItem.DataBoundObjectProperties != null)
            {
                if (LabelItem.DataBoundObjectProperties.Count > 0)
                {
                    if (LabelItem.DataBoundFieldsFormatString != string.Empty)
                    {

                    }
                    else
                    {
                        string[] keys = new string[LabelItem.DataBoundObjectProperties.Keys.Count];
                        string[] values = new string[LabelItem.DataBoundObjectProperties.Values.Count];
                        LabelItem.DataBoundObjectProperties.Keys.CopyTo(keys, 0);
                        LabelItem.DataBoundObjectProperties.Values.CopyTo(values, 0);
                        Object requiredObject = page.BoundObjects[keys[0]];
                        PropertyInfo propertyInfo = requiredObject.GetType().GetProperty(values[0]);
                        string returnedValue = propertyInfo.GetValue(requiredObject, null).ToString();
                        writer.Write(returnedValue);
                    }
                }
                else
                {
                    writer.Write(LabelItem.LabelText);
                }
            }
            else
            {
                writer.Write(LabelItem.LabelText);
            }
            writer.WriteEndTag("span");
            writer.WriteLine();

            return stringWriter.ToString();
        }

        private string ProcessBasicGrid(Model.BasicGrid grid, Page page)
        {
            return null;
        }

        private string ProcessTextBox(Model.TextBox TextBoxItem, Page page)
        {
            string pageHTML = string.Format("<input type=\"Text\" id=\"{0}\" Name=\"{1}\" Title=\"{2}\" value=\"{3}\"></input>", TextBoxItem.Id.ToString(), TextBoxItem.Name, TextBoxItem.Tooltip, TextBoxItem.TextBoxText);
            return pageHTML;
        }

        private string ProcessSubmitButton(Model.SubmitButton SubmitButtonItem, Page page)
        {
            string pageHTML = string.Format("<input type=\"Submit\" id=\"{0}\" Name=\"{1}\" Title=\"{2}\" value=\"{3}\"></input>", SubmitButtonItem.Id.ToString(), SubmitButtonItem.Name, SubmitButtonItem.Tooltip, SubmitButtonItem.ButtonText);
            return pageHTML;
        }
    }
}
